package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import system.Helper;
import system.Model;
import system.Validator;

/**
 * Class GantiPassword
 * 
 * @author Rezan Achmad
 */
public class GantiPassword extends Model {

    public String password_lama;
    public String password_baru;
    public String re_password_baru;
    public User user;

    public GantiPassword(User user) {
        password_lama = "";
        password_baru = "";
        re_password_baru = "";
        this.user = user;
    }

    public boolean execute() {
        return validate() && user.changePassword(password_baru);
    }

    @Override
    public boolean add() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setAttributes(ResultSet rs) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean validate() {
        boolean valid = true;
        Validator validator = Validator.getInstance();
        
        // Validate password lama
        valid = validator.equal(user.password, this, "password_lama", Helper.MD5(password_lama), "", "Password tidak cocok") && valid;

        // Validate password baru
        valid = validator.notEmptyString(password_baru, this, "password_baru") && valid;

        // Validate re password baru
        valid = validator.notEmptyString(re_password_baru, this, "re_password_baru") && valid;
        if(valid) valid = validator.equal(re_password_baru, this, "re_password_baru", password_baru, "password_baru");

        return valid;
    }

    @Override
    public void initAttributesLabel() {
        attributesLabel = new HashMap<String, String>();
        attributesLabel.put("password_baru", "Password Baru");
        attributesLabel.put("re_password_baru", "Re Password Baru");
    }
}
